package com.example.notice_management_p1.service.Impl;

import com.example.notice_management_p1.service.INoticeService;
import com.pojo.Entity.Notice;
import com.pojo.dto.ToNotice;
import com.pojo.mapper.NoticeMapper;
import com.pojo.util.Tool;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class NocticeServiceImpl implements INoticeService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    RedissonClient redissonClient;
    private static final String NOTICE_KEY="NOTICE_KEY_GET:";
    private static final String NOTICE_LOCK_KEY="NOTICE_LOCK_KEY:";
    @Override
    public Integer insert(Notice notice, HttpServletRequest httpServletRequest) {
        String identifying=httpServletRequest.getHeader("identifying");
        Integer uID= (Integer) redisTemplate.opsForHash().get(identifying,"uID");
        notice.setNDate(new Date());
        notice.setNRid(uID);
        Integer count= noticeMapper.insert(notice);
        if(count==1)
        redisTemplate.opsForValue().set(NOTICE_KEY+String.valueOf(notice.getNID()),notice,120, TimeUnit.MINUTES);
        return count;
    }

    @Override
    public Integer update(Notice notice, HttpServletRequest httpServletRequest) {
        String identifying=httpServletRequest.getHeader("identifying");
        Integer uID= (Integer) redisTemplate.opsForHash().get(identifying,"uID");
        notice.setNDate(new Date());
        notice.setNRid(uID);
        String nky=NOTICE_KEY+String.valueOf(notice.getNID());
        String lnky=NOTICE_LOCK_KEY+String.valueOf(notice.getNID());
        try {
            return Tool.updateToRedis(site1->noticeMapper.update(site1),notice,nky,lnky,redisTemplate,redissonClient);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<ToNotice> getlnotice(Integer Page, Integer Number) {
        Page=(Page-1)*Number;
        return noticeMapper.findBypn(Page,Number);
    }

    @Override
    public Notice getnotice(Integer nID) {
        String nky=NOTICE_KEY+String.valueOf(nID);
        String lnky=NOTICE_LOCK_KEY+String.valueOf(nID);
        Notice notice= (Notice) redisTemplate.opsForValue().get(nky);
        if(notice==null){
            try {
                notice = (Notice) Tool.putDateToRedis(nID1 -> noticeMapper.findBynID(nID1), nID, nky,lnky,new Notice(),redisTemplate,redissonClient);
                return notice;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return notice;
    }

    @Override
    public Integer delete(Integer nID) {
        redisTemplate.delete( NOTICE_KEY+String.valueOf(nID));
        return noticeMapper.delete(nID);
    }

    @Override
    public Integer getsum() {
        return noticeMapper.getsum();
    }
}
